package hn.cch.indicator;


import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;

import java.util.List;

/**
 * 风险价值Value At Risk
 */
public class VaR implements UnivariateFunction {


    @Override
    public double value(double v) {
        return 0;
    }


    public double calculate(Double VAR, List<Double> meanRORs, Double confidenceInterval){

        // List<Double> 转 double[]
        double[] rors = new double[meanRORs.size()];
        for (int i = 0; i < meanRORs.size(); i++) {
            rors[i] = meanRORs.get(i);
        }

        // 标准差
        StandardDeviation standardDeviation = new StandardDeviation();
        standardDeviation.setBiasCorrected(false);

        double std = standardDeviation.evaluate(rors);
        double sigema = Math.sqrt(Math.pow(1 + confidenceInterval, 2) - 1) * std;

        double sum = 0;
        for (double mean:meanRORs) {
            // 正态分布
            NormalDistribution normalDistribution = new NormalDistribution(mean, sigema);
            // 累计概率
            double cdf = normalDistribution.cumulativeProbability(VAR);
            sum += cdf;
        }
        return sum / meanRORs.size();



    }







}
